//
//  LGMainInspectAddController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/1.
//  核心检查项 - 添加、编辑

#import "LGMainInspectAddController.h"
//查看汇总
#import "LGMainInspectItemController.h"
//检查项趋势
#import "LGInspectChartController.h"
//区头
#import "LGPatientAttendHeader.h"
#import "LGMainInspectHeaderCell.h"
//检查结果
#import "LGMainInspectNormalCell.h"
#import "LGMainInspectNormalEditCell.h"
//诊断文本
#import "LGMainInspectStringCell.h"
#import "LGPatientAttendStringEditCell.h"
//心脏彩超
#import "LGMainHeartUltrasoundCell.h"
//纽约心功能分级
#import "LGPatientAttendClassificationCell.h"
//评估
#import "LGPatientAttendEvaluateCell.h"
#import "LGPatientAttendModel.h"
@interface LGMainInspectAddController ()<UITableViewDataSource,UITableViewDelegate,LGPatientAttendHeaderDelegate,LGMainInspectNormalEditDelegate,LGPatientAttendStringEditDelegate,LGMainHeartUltrasoundDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *sectionArray;
/**  血常规 */
@property (nonatomic , strong)NSMutableArray *reportArray1;
/**  肝功 */
@property (nonatomic , strong)NSMutableArray *reportArray2;
/**  肾功 */
@property (nonatomic , strong)NSMutableArray *reportArray3;
/**  血脂 */
@property (nonatomic , strong)NSMutableArray *reportArray4;
/**  心肌标志物 */
@property (nonatomic , strong)NSMutableArray *reportArray5;
/**  心电图诊断结果 */
@property (nonatomic , strong)NSMutableDictionary *reportDic6;
/**  心脏彩超 */
@property (nonatomic , strong)NSDictionary *reportDic7;
/**   */
@property (nonatomic , strong)NSArray *dateArray;
/**   */
@property (nonatomic , copy)NSString *currentDate;
/**   */
@property (nonatomic , strong)NSMutableDictionary *dataDic;
@end

@implementation LGMainInspectAddController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"编辑核心指标汇总";
    NSArray *tempArray = @[@{@"title":@"血常规",@"opened":@(YES)},@{@"title":@"生化",@"opened":@(YES)},@{@"title":@"血脂",@"opened":@(YES)},@{@"title":@"心肌标志物",@"opened":@(YES)},@{@"title":@"心电图",@"opened":@(YES)},@{@"title":@"心脏彩超",@"opened":@(YES)}];
    self.sectionArray = [LGPatientAttendModel mj_objectArrayWithKeyValuesArray:tempArray];
    [self.view addSubview:self.tableView];
    [self createBottomView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.care_plan/careplanVisitReport" parameters:@{@"patient_id":self.patientid,@"visit_date":self.fullowDate} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataDic = [NSMutableDictionary dictionaryWithDictionary:returnData];
            self.reportArray1 = [NSMutableArray arrayWithArray:returnData[@"report1"]];
            self.reportArray2 = [NSMutableArray arrayWithArray:returnData[@"report2"]];
            self.reportArray3 = [NSMutableArray arrayWithArray:returnData[@"report3"]];
            self.reportArray4 = [NSMutableArray arrayWithArray:returnData[@"report4"]];
            self.reportArray5 = [NSMutableArray arrayWithArray:returnData[@"report5"]];
            self.reportDic6 = [NSMutableDictionary dictionaryWithDictionary:returnData[@"report6"]];
            self.reportDic7 = returnData[@"report7"];
            LGPatientAttendModel *model = self.sectionArray[4];
            model.opened = (LGNSString(self.reportDic6[@"content"]).length>0 || [LGNSString(self.reportDic6[@"is_editable"]) isEqualToString:@"1"])?YES:NO;
            [self.sectionArray replaceObjectAtIndex:4 withObject:model];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)saveBtnAction{
    [self.dataDic setValue:self.reportArray1 forKey:@"report1"];
    [self.dataDic setValue:self.reportArray2 forKey:@"report2"];
    [self.dataDic setValue:self.reportArray3 forKey:@"report3"];
    [self.dataDic setValue:self.reportArray4 forKey:@"report4"];
    [self.dataDic setValue:self.reportArray5 forKey:@"report5"];
    [self.dataDic setValue:self.reportDic6 forKey:@"report6"];
    [self.dataDic setValue:self.reportDic7 forKey:@"report7"];
    [RequestUtil withPOST:@"/doctor/Visitrecords/savePatientCoreInspection" parameters:@{@"patient_id":self.patientid,@"base_date":self.fullowDate,@"content_json":[ObjectTool objectToJson:self.dataDic]} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"保存成功" offset:0];
            LGMainInspectItemController *controller = [[LGMainInspectItemController alloc]init];
            controller.patientid = self.patientid;
            controller.fullowDate = self.fullowDate;
            [self.navigationController pushViewController:controller animated:YES];
            
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
    }];
}

-(void)openCloseHeaderView:(BOOL)open section:(NSInteger)section{
    LGPatientAttendModel *model = self.sectionArray[section];
    model.opened = open;
    [self.sectionArray replaceObjectAtIndex:section withObject:model];
//    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView reloadData];
}

-(void)mainInspectEditChanged:(NSDictionary *)tempDic index:(NSInteger)index key:(NSString *)key{
    if([key isEqualToString:@"report1"]){
        [self.reportArray1 replaceObjectAtIndex:index withObject:tempDic];
        
    }else if([key isEqualToString:@"report2"]){
        [self.reportArray2 replaceObjectAtIndex:index withObject:tempDic];
        
    }else if([key isEqualToString:@"report3"]){
        [self.reportArray3 replaceObjectAtIndex:index withObject:tempDic];
        
    }else if([key isEqualToString:@"report4"]){
        [self.reportArray4 replaceObjectAtIndex:index withObject:tempDic];
        
    }else if([key isEqualToString:@"report5"]){
        [self.reportArray5 replaceObjectAtIndex:index withObject:tempDic];
        
    }else if([key isEqualToString:@"report6"]){
        [self.reportDic6 setValue:LGNSString(tempDic[@"content"]) forKey:@"content"];
        
    }else if([key isEqualToString:@"report7"]){
        self.reportDic7 = tempDic;
    }
}


#pragma mark
#pragma mark ====> tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    LGPatientAttendModel *model = self.sectionArray[section];
    if(section == 0){
        //血常规
        return (model.opened == YES)?self.reportArray1.count+1:0;
        
    }else if (section == 1){
        //生化
        NSInteger count = 0;
        if(self.reportArray2.count>0){
            count += self.reportArray2.count+1;
        }
        
        if(self.reportArray3.count>0){
            count += self.reportArray3.count+1;
        }
        return (model.opened == YES)?count:0;;
        
    }else if (section == 2){
        //血脂
        return (model.opened == YES)?self.reportArray4.count+1:0;
        
    }else if (section == 3){
        //心肌标志物
        return (model.opened == YES)?self.reportArray5.count+1:0;
        
    }else if (section == 4){
        //心电图
        return (model.opened == YES && (LGNSString(self.reportDic6[@"content"]).length>0 || [LGNSString(self.reportDic6[@"is_editable"]) isEqualToString:@"1"]))?1:0;
        
    }else if (section == 5){
        //心脏彩超
        return (model.opened == YES)?1:0;
        
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        //血常规
        if(indexPath.row == 0){
            LGMainInspectHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
            headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
            headerCell.title = @"";
            return headerCell;
        }else{
            NSDictionary *tempDic = self.reportArray1[indexPath.row - 1];
            if([LGNSString(tempDic[@"is_editable"]) isEqualToString:@"1"]){
                //可编辑
                LGMainInspectNormalEditCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"editCell" forIndexPath:indexPath];
                editCell.selectionStyle = UITableViewCellSelectionStyleNone;
                editCell.dataDic = tempDic;
                editCell.index = indexPath.row-1;
                editCell.key = @"report1";
                editCell.delegate = self;
                return editCell;
                
            }else{
                //不可编辑
                LGMainInspectNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
                normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
                normalCell.dataDic = tempDic;
                return normalCell;
            }
        }
    }else if(indexPath.section == 1){
        if(self.reportArray2.count>0){
            if(indexPath.row == 0 || indexPath.row == self.reportArray2.count+1){
                LGMainInspectHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
                headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
                headerCell.title = (indexPath.row == 0)?@"肝功":@"肾功";
                return headerCell;
                
            }else{
                NSDictionary *tempDic;
                NSInteger index = 0;
                NSString *key = @"";
                if(indexPath.row-1<self.reportArray2.count){
                    key = @"report2";
                    index = indexPath.row-1;
                    tempDic = self.reportArray2[indexPath.row-1];
                }else{
                    key = @"report3";
                    index = indexPath.row-self.reportArray2.count-2;
                    tempDic = self.reportArray3[indexPath.row-self.reportArray2.count-2];
                }
                if([LGNSString(tempDic[@"is_editable"]) isEqualToString:@"1"]){
                    //可编辑
                    LGMainInspectNormalEditCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"editCell" forIndexPath:indexPath];
                    editCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    editCell.dataDic = tempDic;
                    editCell.index = index;
                    editCell.key = key;
                    editCell.delegate = self;
                    return editCell;
                    
                }else{
                    //不可编辑
                    LGMainInspectNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
                    normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    normalCell.dataDic = tempDic;
                    return normalCell;
                }
            }
        }else{
            if(indexPath.row == 0){
                LGMainInspectHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
                headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
                headerCell.title = @"肾功";
                return headerCell;
                
            }else{
                NSDictionary *tempDic = self.reportArray3[indexPath.row - 1];
                if([LGNSString(tempDic[@"is_editable"]) isEqualToString:@"1"]){
                    //可编辑
                    LGMainInspectNormalEditCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"editCell" forIndexPath:indexPath];
                    editCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    editCell.dataDic = tempDic;
                    editCell.index = indexPath.row-1;
                    editCell.key = @"report3";
                    editCell.delegate = self;
                    return editCell;
                    
                }else{
                    //不可编辑
                    LGMainInspectNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
                    normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
                    normalCell.dataDic = tempDic;
                    return normalCell;
                }
            }
        }
        
    }else if (indexPath.section == 2){
        //血脂
        if(indexPath.row == 0){
            LGMainInspectHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
            headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
            headerCell.title = @"";
            return headerCell;
        }else{
            NSDictionary *tempDic = self.reportArray4[indexPath.row - 1];
            if([LGNSString(tempDic[@"is_editable"]) isEqualToString:@"1"]){
                //可编辑
                LGMainInspectNormalEditCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"editCell" forIndexPath:indexPath];
                editCell.selectionStyle = UITableViewCellSelectionStyleNone;
                editCell.dataDic = tempDic;
                editCell.index = indexPath.row-1;
                editCell.key = @"report4";
                editCell.delegate = self;
                return editCell;
                
            }else{
                //不可编辑
                LGMainInspectNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
                normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
                normalCell.dataDic = tempDic;
                return normalCell;
            }
        }
    }else if (indexPath.section == 3){
        //心肌标志物
        if(indexPath.row == 0){
            LGMainInspectHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
            headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
            headerCell.title = @"";
            return headerCell;
        }else{
            NSDictionary *tempDic = self.reportArray5[indexPath.row - 1];
            if([LGNSString(tempDic[@"is_editable"]) isEqualToString:@"1"]){
                //可编辑
                LGMainInspectNormalEditCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"editCell" forIndexPath:indexPath];
                editCell.selectionStyle = UITableViewCellSelectionStyleNone;
                editCell.dataDic = tempDic;
                editCell.index = indexPath.row-1;
                editCell.key = @"report5";
                editCell.delegate = self;
                return editCell;
                
            }else{
                //不可编辑
                LGMainInspectNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
                normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
                normalCell.dataDic = tempDic;
                return normalCell;
            }
        }
        
    }else if (indexPath.section == 4){
        //心电图
        if([LGNSString(self.reportDic6[@"is_editable"]) isEqualToString:@"1"]){
            //可编辑
            LGPatientAttendStringEditCell *editCell = [tableView dequeueReusableCellWithIdentifier:@"stringEditCell" forIndexPath:indexPath];
            editCell.selectionStyle = UITableViewCellSelectionStyleNone;
            editCell.content = LGNSString(self.reportDic6[@"content"]);
            editCell.tableView = self.tableView;
            editCell.delegate = self;
            return editCell;
            
        }else{
            //不可编辑
            LGMainInspectStringCell *stringCell = [tableView dequeueReusableCellWithIdentifier:@"stringCell" forIndexPath:indexPath];
            stringCell.selectionStyle = UITableViewCellSelectionStyleNone;
            stringCell.content = LGNSString(self.reportDic6[@"content"]);
            return stringCell;
   
        }

    }else if (indexPath.section == 5){
        //心脏彩超
        LGMainHeartUltrasoundCell *ultrasoundCell = [tableView dequeueReusableCellWithIdentifier:@"ultrasoundCell" forIndexPath:indexPath];
        ultrasoundCell.selectionStyle = UITableViewCellSelectionStyleNone;
        ultrasoundCell.tableView = self.tableView;
        ultrasoundCell.dataDic = self.reportDic7;
        ultrasoundCell.delegate = self;
        return ultrasoundCell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //检查项趋势
    NSString *itemCode = @"";
    NSString *patientid = @"";
    NSString *patientNum = @"";
    
    if(indexPath.section == 0 && indexPath.row > 0){
        NSDictionary *tempDic = self.reportArray1[indexPath.row-1];
        itemCode = LGNSString(tempDic[@"item_code"]);
        patientNum = LGNSString(tempDic[@"hospital_bano"]);
        patientid = LGNSString(tempDic[@"patient_id"]);
        
    }else if (indexPath.section == 1 && indexPath.row > 0){
        if(self.reportArray2.count>0 && indexPath.row != 0 && indexPath.row != self.reportArray2.count+1){
            if(indexPath.row-1<self.reportArray2.count){
                NSDictionary *tempDic = self.reportArray2[indexPath.row-1];
                itemCode = LGNSString(tempDic[@"item_code"]);
                patientNum = LGNSString(tempDic[@"hospital_bano"]);
                patientid = LGNSString(tempDic[@"patient_id"]);
            }else{
                NSDictionary *tempDic = self.reportArray3[indexPath.row-self.reportArray2.count-2];
                itemCode = LGNSString(tempDic[@"item_code"]);
                patientNum = LGNSString(tempDic[@"hospital_bano"]);
                patientid = LGNSString(tempDic[@"patient_id"]);
            }
        }else if(indexPath.row != 0){
            NSDictionary *tempDic = self.reportArray3[indexPath.row-1];
            itemCode = LGNSString(tempDic[@"item_code"]);
            patientNum = LGNSString(tempDic[@"hospital_bano"]);
            patientid = LGNSString(tempDic[@"patient_id"]);
        }
    }else if (indexPath.section == 2 && indexPath.row > 0){
        NSDictionary *tempDic = self.reportArray4[indexPath.row-1];
        itemCode = LGNSString(tempDic[@"item_code"]);
        patientNum = LGNSString(tempDic[@"hospital_bano"]);
        patientid = LGNSString(tempDic[@"patient_id"]);
        
    }else if (indexPath.section == 3 && indexPath.row > 0){
        NSDictionary *tempDic = self.reportArray5[indexPath.row-1];
        itemCode = LGNSString(tempDic[@"item_code"]);
        patientNum = LGNSString(tempDic[@"hospital_bano"]);
        patientid = LGNSString(tempDic[@"patient_id"]);
    }
    
    if(itemCode.length>0){
        LGInspectChartController *controller = [[LGInspectChartController alloc]init];
        controller.code = itemCode;
        controller.patientid = patientid;
        controller.patientNum = patientNum;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(50);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGPatientAttendModel *model = self.sectionArray[section];
    LGPatientAttendHeader *headerView = [[LGPatientAttendHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    headerView.delegate = self;
    headerView.section = section;
    headerView.title = model.title;
    headerView.opened = model.opened;
    
    BOOL finished = NO;
    if(section == 0 && self.reportArray1.count>0){
        //血常规
        finished = YES;
        
    }else if (section == 1){
        //生化
        NSInteger count = 0;
        if(self.reportArray2.count>0){
            count += self.reportArray2.count+1;
        }
        
        if(self.reportArray3.count>0){
            count += self.reportArray3.count+1;
        }
        
        finished = (count>0)?YES:NO;
        
    }else if (section == 2 && self.reportArray4.count>0){
        //血脂
        finished = YES;
        
    }else if (section == 3 && self.reportArray5.count>0){
        //心肌标志物
        finished = YES;
        
    }else if (section == 4 && (LGNSString(self.reportDic6[@"content"]).length>0 || [LGNSString(self.reportDic6[@"is_editable"]) isEqualToString:@"1"])){
        //心电图
        finished = YES;
        
    }else if (section == 5 && [self.reportDic7 allKeys].count>0){
        //心脏彩超
        finished = YES;
        
    }
    headerView.finished = finished;
//    [headerView.checkBtn addTarget:self action:@selector(checkHomeMonitoringAction) forControlEvents:UIControlEventTouchUpInside];
    return headerView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(20);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(20))];
    footerView.backgroundColor = [UIColor clearColor];
    footerView.bottomMargin = viewPix(10);
    footerView.shadowView.layer.cornerRadius = 15;
    return footerView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight-viewPix(72)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGMainInspectHeaderCell class] forCellReuseIdentifier:@"headerCell"];
        [_tableView registerClass:[LGMainInspectNormalCell class] forCellReuseIdentifier:@"normalCell"];
        [_tableView registerClass:[LGMainInspectNormalEditCell class] forCellReuseIdentifier:@"editCell"];
        [_tableView registerClass:[LGMainInspectStringCell class] forCellReuseIdentifier:@"stringCell"];
        [_tableView registerClass:[LGPatientAttendStringEditCell class] forCellReuseIdentifier:@"stringEditCell"];
        [_tableView registerClass:[LGMainHeartUltrasoundCell class] forCellReuseIdentifier:@"ultrasoundCell"];
        [_tableView registerClass:[LGPatientAttendClassificationCell class] forCellReuseIdentifier:@"classificationCell"];
        [_tableView registerClass:[LGPatientAttendEvaluateCell class] forCellReuseIdentifier:@"evaluateCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(35);
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(void)createBottomView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, viewPix(72)+bottomSafeBarHeight/4.0)];
    baseView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:baseView];
    UIButton *saveBtn = [UIButton buttonWithTitle:@"保存" topMargin:viewPix(8)];
    [saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:saveBtn];
}



@end
